<template>
	<view class="search-area">
		<uni-search-bar class="search-bar" @confirm="search" @input="input" radius="50" cancelButton="none" :focus="true" bgColor="#fff"></uni-search-bar>
	</view>
	<!--有内容-->
	<view class="search-list" v-if="searchList.length!=0">
		<view v-for="(item,index) in searchList" :key="index" class="search-item" @click="goGoodsDetail(item)">
			<view class="search-title">{{item.goods_name}}</view>
			<uni-icons type="right" size="12"></uni-icons>
		</view>
	</view>
	<view class="search-history" v-else>
		<!--上边的盒子-->
		<view class="search-history-title">
			<view class="">搜索历史</view>
			<view @click="clearHistory">
				<uni-icons type="trash" size="13"></uni-icons>
			</view>
		</view>
		<!--下边的盒子-->
		<view class="search-history-content">
			<view class="search-history-item" v-for="(item,index) in historyList" :key="index" @click="goGoodsList(item)">{{item}}</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				keyWord: '', // 输入内容
				timer: null,
				searchList: [], // 搜索列表
				historyList: [], //搜索历史
			};
		},
		onLoad(){
			// 获取缓存中的关键字
			this.historyList = JSON.parse(uni.getStorageSync('kw')||'[]')
			// console.log('this.history',this.historyList)	
		},
		computed:{
			/* historys(){
				return [...this.historyList].reverse()
			} */
		},
		methods:{
			search(e){
				// console.log('搜索',e)
				this.getGoodsList()
			},
			input(e){
				// 做一个防抖处理 固定时间后再显示输入的内容
				if(this.timer){
					clearTimeout(this.timer)
					this.timer = null
				}
				this.timer = setTimeout(()=> {
					this.keyWord = e
					// this.historyList.push(e)
					this.getGoodsList()
					this.timer = null
				}, 500);
			},
			// 处理历史记录
			handlerHistory(e){
				// let arr = this.historyList
				// arr.push(e)
				// this.historyList = arr.reverse()
				this.historyList.unshift(e)
				// 去重
				this.historyList = [...new Set(this.historyList)];
				// 保存到session中
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			// 清空历史搜索记录
			clearHistory(){
				this.historyList = [];
				uni.setStorageSync('kw', '[]')
			},
			async getGoodsList(){
				// console.log('key',this.keyWord)
				if(!this.keyWord){
					this.searchList = [];
					return
				}
				const {data:res} = await uni.$http.get(uni.api.Q_SEARCH,{query: this.keyWord})
				if(res.meta.status !== 200) return uni.$showMsg();
				this.searchList = res.message
				// 把搜索记录存入搜索历史
				this.handlerHistory(this.keyWord)
				// console.log('res',res,res.message,this.searchList)
			},
			// 去商品详情页
			goGoodsDetail(item){
				uni.navigateTo({
					url:"/subpkg/goods_detail/goods_detail?goods_id="+item.goods_id
				})
			},
			// 去商品列表页
			goGoodsList(kw){
				uni.navigateTo({
					url:"/subpkg/goods_list/goods_list?query="+kw
				})
			}
		}
	}
</script>

<style lang="scss">
page{
	background: #fff;
}
.search-area{
	position:sticky;
	top:0;
	z-index:999;
	background-color:#c00000;
	padding: 0 20rpx;
	height: 100rpx;
	display:flex;
	justify-content:center;
	align-items:center;
	box-sizing:border-box;
	.search-bar{
		width: 100%;
		height:100%;
	}
	
}
.search-bar .uni-searchbar__box{
		height: 30px;
}
	
/* 搜索出来列表的区域 */
.search-list{
	padding: 0 5px;
	
	.search-item{
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 80rpx;
		border-bottom: 1px solid #efefef;
		font-size: 24rpx;
		.search-title{
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
		}
	}
}
.search-history{
	padding: 0 10rpx 20rpx;
	.search-history-title{
		display: flex;
		justify-content: space-between;
		font-size: 26rpx;
		padding: 20rpx 10rpx;
		border-bottom: 1px solid #eee;
		margin-bottom: 20rpx;
	}
	.search-history-content{
		display: flex;
		flex-wrap: wrap;
		.search-history-item{
			padding: 10rpx;
			background: #eee;
			border-radius: 6px;
			font-size: 24rpx;
			margin-right: 24rpx;
		}
	}
}
</style>
